"use strict";

angular.module('app.yksensor').controller('SensorProtocolCtrl', function ($scope,sensorHttpS) {

  //配置分页基本参数
   $scope.paginationConf = {
       currentPage: 1,
       itemsPerPage: 10,
       pagesLength: 5,
       totalItems: 20,
       perPageOptions: [10, 20, 30, 40, 50],
       onChange: function () {
       }
   };

    $scope.viewProtocol = function (id,name) {
        
        window.location.href = appConfig.base[0].baseURI + "#/sensorproc/" + id;

      }
    // 得到所有的协议
    var GetData = function () {
        sensorHttpS.postAll("getProtocolPagi",$scope.paginationConf).then(function(response){
           $scope.sensorProtocolInfo = response.content;
           $scope.paginationConf.totalItems = response.sum;
        });

    }
    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', GetData);
    $scope.sensorProtocol = {};
    // 添加协议
    $scope.addSensorProtocolF = function () {
        // $scope.sensorProtocol.company_id = $scope.currentCompany.id;
        $('#myModal').modal('hide'); 
        var dataobj = {};
        dataobj.oper = "add";   
        dataobj.data = $scope.sensorProtocol;
        console.log(dataobj);
        sensorHttpS.postAll('editProtocol',dataobj).then(function (response) {
            if (response > 0) {
                GetData();
                $.smallBox({
                    title: "添加协议成功",
                    content: "<i class='fa fa-clock-o'></i> <i>添加协议成功</i>",
                    color: "#659265",
                    iconSmall: "fa fa-check fa-2x fadeInRight animated",
                    timeout: 4000
                });
            }
            else {

            }

        })

    };

    $scope.editSensorProtocol = {};
    $scope.showEditSensorProtocol = function (item) {
        $scope.editSensorProtocol.id = item.id;
        $scope.editSensorProtocol.name = item.name;
        $scope.editSensorProtocol.des = item.des;
        $('#myModalEdit').modal('show');

    };
    // 修改协议
    $scope.editSensorProtocolF = function () {
        $('#myModalEdit').modal('hide');
        // $scope.editSensorProtocol.company_id = $scope.currentCompany.id;
        var dataobj = {};
        dataobj.oper = "edit";
        dataobj.data = $scope.editSensorProtocol;
        console.log(dataobj);
        sensorHttpS.postAll('editProtocol', dataobj).then(function (response) {
            if (response > 0) {

                 GetData();
                $.smallBox({
                    title: "编辑协议成功",
                    content: "<i class='fa fa-clock-o'></i> <i>编辑协议成功</i>",
                    color: "#659265",
                    iconSmall: "fa fa-check fa-2x fadeInRight animated",
                    timeout: 4000
                });
            }
            else {

            }

        })

    }

    $scope.delSensorProtocol = {};
    $scope.showDelDialog = function (id) {
        $scope.delSensorProtocol.id = id;
        
        $('#showDelDialog').modal('show');

    }

    $scope.delSensorProtocolF = function () {
        $('#showDelDialog').modal('hide'); 
        var dataobj = {};
        dataobj.oper = "del";
        dataobj.data = $scope.delSensorProtocol;
        console.log(dataobj); 
        sensorHttpS.postAll('editProtocol',dataobj).then(function (response) {
            if (response > 0) {
                GetData();
                 $.smallBox({
                    title: "删除协议成功",
                    content: "<i class='fa fa-clock-o'></i> <i>删除协议成功</i>",
                    color: "#659265",
                    iconSmall: "fa fa-check fa-2x fadeInRight animated",
                    timeout: 4000
                });

            }
            else {

            }

        })
    }

});